package org.example.register;

import org.junit.Before;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

//注册功能测试
public class SignTest {
    public static WebDriver webDriver;
    @BeforeAll
    public static void before(){
        webDriver = new ChromeDriver();
        webDriver.get("http://123.60.88.108:8080/register.html");
    }
    @ParameterizedTest
    @CsvFileSource(resources = "/register.csv", numLinesToSkip = 1)
    public void test(String username, String password) throws InterruptedException {

        Thread.sleep(3000);
        //定位用户名和密码输入框
        WebElement element = webDriver.findElement(By.cssSelector("#username"));
        WebElement element1 = webDriver.findElement(By.cssSelector("#password"));
        //清空用户名和密码框
        element.clear();
        element1.clear();
        //输入内容
        element.sendKeys(username);
        element1.sendKeys(password);
        WebElement button = webDriver.findElement(By.cssSelector("#signIn"));
        button.click();
        Thread.sleep(2000);
        Alert alert = webDriver.switchTo().alert();
        alert.accept();
        Thread.sleep(3000);
        //获取当前页面的url
        String currentUrl = webDriver.getCurrentUrl();
        String aimUrl = "http://123.60.88.108:8080/login.html";
        if(currentUrl.equals(aimUrl)) {
            System.out.println("测试成功：注册成功");
        }else{
            System.out.println("测试成功：注册失败");
        }
    }
}
